// 初始化分类页面
function initCategory() {
    show(1, 15);
    setCategoryModal();
}

// 渲染分页查询的分类
function show(pageNum, pageSize) {
    var tbody = $('#tbody');
    var pageLink = $('#pageLink');
    if(!pageNum){
        pageNum = pageLink.data('page').pageNum;
    }
    if(!pageSize){
        pageSize = pageLink.data('page').pageSize;
    }

    var getData = pageCategory(pageNum, pageSize);

    if(pageNum > getData.data.pages && pageNum != 1){
        show(getData.data.pages, pageSize);
        return;
    }

    tbody.children().remove();
    var index = 1;
    getData.data.records.forEach(item => {
        const row = '<tr>\n' +
            '                            <th scope="row">' +
            '                               <input class="checkBox form-check-input" type="checkbox" id="check" name="option1" value="' + item.id + '">\n' +
            '                           </th>     ' +
            '                            <td><span class="d-flex justify-content-center"> ' + index + '</span></td>' +
            '                            <td><span class="d-flex justify-content-center">' + item.code + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">' + item.name + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">' + item.createTime.replace("T", " ") + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">' + item.updateTime.replace("T", " ") + '</span></td>\n' +
            '                            <td><span class="d-flex justify-content-center">\n' +
            '                                <span class="dropdown">\n' +
            '                                  <button class="btn dropdown-toggle" data-bs-boundary="viewport"\n' +
            '                                          data-bs-toggle="dropdown">操作</button>\n' +
            '                                    <ul class="dropdown-menu "\n' +
            '                                        style="min-width: 1px;">\n' +
            '                                        <li>\n' +
            '                                            <button  class="dropdown-item" data-bs-toggle="modal" data-bs-target="#itemModal" data-bs-type="edit" onclick="updateCategoryModal(this)" value="' + item.id + '">编辑</button>\n' +
            '                                        </li>\n' +
            '                                        <li>\n' +
            '                                <button class="dropdown-item" onclick="deleteCategory(this)" value="' + item.id + '">删除</button>\n' +
            '                                        </li>\n' +
            '                                    </ul>\n' +
            '                                </span>\n' +
            '                             </span></td>\n' +
            '                        </tr>';
        tbody.append(row);
        index++;
    });

    pageLink.data("page", {
        pages: getData.data.pages,
        pageNum: pageNum,
        pageSize: pageSize,
    });
    // 设置选择头
    $('#checkHead').prop('checked', false);
    setPageFront( (pageNum - 1) * pageSize + 1,pageNum * pageSize > getData.data.total ? getData.data.total: pageNum * pageSize, getData.data.total);
    $('td').addClass('align-middle');
    loadPagingLabel(pageLink);
    setPagingLabel(pageLink);
    $('.page-item').on('click', function () {
        pageItem(this);
    });
}



// 设置表单的值
function setCategoryModal() {
    const itemModal = document.getElementById('itemModal');
    if (itemModal) {
        itemModal.addEventListener('show.bs.modal', event => {
            document.querySelector('#categoryForm').reset();
            const button = event.relatedTarget;
            const type = button.getAttribute('data-bs-type');
            const modalTitle = $('#modalTitle');
            const categoryModalSubmit = $('#categoryModalSubmit');
            if (type == 'add') {
                modalTitle.text('添加商品');
                categoryModalSubmit.text('添加');
                $('#categoryCodeDiv').show();
                categoryModalSubmit.off('click').on('click', function () {
                    addCategory();
                });
            } else if (type == 'edit') {
                modalTitle.text('修改商品');
                categoryModalSubmit.text('保存');
                $('#categoryCodeDiv').hide();
                categoryModalSubmit.off('click').on('click', function () {
                    updateCategory();
                });
            }
        });
    }
}

// 商品上传表单回显数据
function updateCategoryModal(e) {
    // 获取数据
    var getData = idGetCategory($(e).val());
    $('#categoryId').val(getData.data.id);
    $('#categoryName').val(getData.data.name);
}


